package ace.game.level;

import ace.game.entity.enemy.EnemyData;
import ace.game.entity.pickup.Pickup;
import ace.game.entity.pickup.PickupFactory;
import ace.geometry.Vertex2D;
import java.util.Vector;

import ace.system.xml.XmlNode;
import ace.system.xml.XmlReaderWriter;

public class Level {
	protected Level() {
		//Initialize
		m_Name		= "";
		m_Index		= 0;

		//Empty
		m_Map		= null;
		m_Dialogs	= new Vector();
        m_Pickups   = new Vector();
	}

	public Level(String file) {
		//Default
		this();
		
		//Temporary data
		String Set 	= null;
		String Map	= null;
		
		//Read XML	
		XmlNode xmlNode = XmlReaderWriter.getInstance().readFromXmlFile(file);
		
		//For each child
		for(int i=0; i < xmlNode.getChildren().size(); ++i) {
			//Get node
			XmlNode Node	= (XmlNode)xmlNode.getChildren().elementAt(i);
			String Tag 		= Node.getNodeName();
			String Value 	= ((XmlNode)Node.getChildren().elementAt(0)).getNodeValue();
            
            if (Tag.equals("index"))		m_Index		= Integer.parseInt(Value);
			else if (Tag.equals("gold"))	m_StartGold	= Integer.parseInt(Value);
            else if (Tag.equals("life"))	m_StartLife	= Integer.parseInt(Value);
            else if (Tag.equals("name"))	m_Name		= Value;
            else if (Tag.equals("tileset"))	Set			= Value;
            else if (Tag.equals("map"))		Map			= Value;
			else if (Tag.equals("enemies")) m_EnemyWaves = new EnemyWaves(Node);
            else if (Tag.equals("pickups")) {
                for (int j = 0, m = Node.getChildren().size(); j < m; ++j) {
                    XmlNode CNode	= (XmlNode)Node.getChildren().elementAt(j);
                    String CTag     = CNode.getNodeName();
                    if (CTag.equals("pickup")) {
                        final int x = Integer.parseInt(CNode.getAttribute("x"));
                        final int y = Integer.parseInt(CNode.getAttribute("y"));
                        final int data = Integer.parseInt(CNode.getAttribute("data"));
                        System.out.println("pickup di " + x + " , " + y + " data = " + data);
                        m_Pickups.addElement(new Pickup(PickupFactory.instance().getPickupData(data), new Vertex2D(x, y)));
                    }
                }
            }
        }
        //Create map
        if (Set != null && Map != null) m_Map = new Map(Map, Set);
	}

	//Accessors
	public Map getMap()					{	return m_Map;                       }
	public int getGold()				{	return m_StartGold;                 }
    public int getLife()				{	return m_StartLife;                 }
    public Vector getPickups()          {   return m_Pickups;                   }

    public Vector getEnemyWaves() {
        if (m_EnemyWaves == null) return new Vector();
        return m_EnemyWaves.getEnemies();
    }

	//Data
	protected String	m_Name;
	protected int		m_Index;
	protected Vector	m_Dialogs;
	protected int		m_StartGold;
    protected int       m_StartLife;

	//Entities
	protected Map m_Map;
	protected EnemyWaves m_EnemyWaves;
    protected Vector m_Pickups;
}
